package com.relytosoft.mizar.media.analyze.services.service.system.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyVehicleGroup;
import com.relytosoft.mizar.media.analyze.services.mapper.system.AyVehicleGroupMapper;
import com.relytosoft.mizar.media.analyze.services.service.system.AyVehicleGroupService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lj
 * @date 2022/5/12 10:17
 */
@Service
public class AyVehicleGroupServiceImpl extends ServiceImpl<AyVehicleGroupMapper, AyVehicleGroup> implements AyVehicleGroupService {


    @Override
    public List<Integer> selectByVehicleId(Integer vehicleId) {
        LambdaQueryWrapper<AyVehicleGroup> wrapper = new QueryWrapper<AyVehicleGroup>().lambda();
        wrapper.eq(AyVehicleGroup::getVehicleId, vehicleId);
        List<AyVehicleGroup> ayPersonGroupList = this.list(wrapper);
        return ayPersonGroupList.stream().map(AyVehicleGroup::getGroupId).collect(Collectors.toList());
    }

    @Override
    public boolean selectByVehicleIdAndGroupIds(Integer vehicleId, List<Integer> oldGroups) {
        if (oldGroups.size() > 0 && vehicleId != null) {
            LambdaQueryWrapper<AyVehicleGroup> wrapper = new QueryWrapper<AyVehicleGroup>().lambda();
            wrapper
                    .eq(AyVehicleGroup::getVehicleId, vehicleId)
                    .in(AyVehicleGroup::getGroupId, oldGroups);
            return this.remove(wrapper);
        }
        return false;
    }

    @Override
    public AyVehicleGroup selectVehicleIdAndGroupId(Integer vehicleId, Integer groupId) {
        LambdaQueryWrapper<AyVehicleGroup> wrapper = new QueryWrapper<AyVehicleGroup>().lambda();
        wrapper.eq(AyVehicleGroup::getVehicleId, vehicleId);
        wrapper.eq(AyVehicleGroup::getGroupId, groupId);
        return this.getOne(wrapper);
    }
}
